package com.ss.android.account.activity.mobile;

import android.content.Context;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.account.b.k;
import com.ss.android.account.bb;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Map;
import org.json.JSONObject;

public final class d {
    static abstract class AbsHttpApiThread extends AbsApiThread {
        protected WeakReference a;
        private WeakHandler b;
        private c c;
        private String d;

        public AbsHttpApiThread(Context arg2, WeakHandler arg3, String arg4, c arg5) {
            this.a = new WeakReference(arg2);
            this.b = arg3;
            this.c = arg5;
            this.d = arg4;
        }

        protected abstract String a(String arg1, Map arg2) throws Exception;

        protected abstract Map a(c arg1);

        protected abstract void a(JSONObject arg1, c arg2) throws Exception;

        public final void run() {
            int v0_1 = 0;
            try {
                if(this.a.get() == null) {
                    this.c.a = 18;
                    v0_1 = 0;
                }else


                if(NetworkUtils.getNetworkType((Context) this.a.get()) == NetworkType.NONE) {
                    this.c.a = 12;
                    v0_1 = 0;
                }else {

                    String v0_2 = this.a(this.d, this.a(this.c));
                    if (android.support.design.a.isTextEmpty(v0_2)) {
                        this.c.a = 18;
                        v0_1 = 0;
                    } else {
                        JSONObject v2 = new JSONObject(v0_2);
                        String v3 = v2.getString("message");
                        if (!"success".equals(v3)) {
                            if ("error".equals(v3)) {
                                v2 = v2.getJSONObject("data");
                                if (v2 != null) {
                                    if ("session_expired".equals(v2.optString("name"))) {
                                        this.c.a = 105;
                                        v0_1 = 0;
                                    } else {
                                        this.c.a = v2.optInt("error_code", this.c.a);
                                        this.c.b = v2.optString("description");
                                        this.c.c = v2.optString("captcha");
                                        this.c.d = v2.optString("alert_text");
                                        if (this.c.a == 1001 && ((this.c instanceof i))) {
                                            v2.optString("dialog_tips");
                                        }
                                    }
                                }
                            } else {
                                Logger.w("AbsHttpApiThread", "request failed: " + this.d + " ->\n" + v0_2);
                                v0_1 = 0;
                            }
                        } else {
                            JSONObject v0_3 = v2.getJSONObject("data");
                            if (v0_3 != null) {
                                this.a(v0_3, this.c);
                            }
                            v0_1 = 1;
                        }
                    }
                }
            } catch(Throwable v0) {
                this.c.a = bb.a().a((Context) this.a.get(), v0);
                v0_1 = 0;
            }

            if(this.b != null) {
                WeakHandler v1 = this.b;
                v0_1 = v0_1 != 0 ? 10 : 11;
                Message v0_4 = v1.obtainMessage(v0_1);
                v0_4.obj = this.c;
                this.b.sendMessage(v0_4);
            }
        }
    }

    static abstract class d$b extends AbsHttpApiThread {
        public d$b(Context arg1, WeakHandler arg2, String arg3, c arg4) {
            super(arg1, arg2, arg3, arg4);
        }

        protected final String a(String arg6, Map arg7) throws Exception {
            ArrayList v2 = new ArrayList();
            if(arg7 != null) {
                Iterator<Entry<String,String>> v3 = arg7.entrySet().iterator();
                while(v3.hasNext()) {
                    Entry<String,String> v0 = v3.next();
                    if(TextUtils.isEmpty( (v0).getKey())) {
                        continue;
                    }

                    if(TextUtils.isEmpty( v0.getValue())) {
                        continue;
                    }

                    ((List)v2).add(new NameValueImpl(v0.getKey(), v0.getValue()));
                }
            }
            return NetworkUtils.executePost(204800, arg6, ((List)v2));
        }
    }

    public static abstract class c {
        public int a;
        public String b;
        public String c;
        public String d;
        public int e;

        public c(int arg1) {

            this.e = arg1;
        }

        public final boolean a() {
            boolean v0;
            if(this.a != 1101 && this.a != 1102 && this.a != 1103) {
                return false;
            } else if(!TextUtils.isEmpty(this.c)) {
                v0 = true;
            }
            else {
            label_14:
                v0 = false;
            }

            return v0;
        }
    }

    public static final class d$d extends d$b {
        public d$d(Context arg3, WeakHandler arg4, String arg5, String arg6) {
            super(arg3, arg4, com.ss.android.account.aa.f, new d$e(arg5, arg6));
        }

        protected final Map a(c arg5) {
            CharSequence v3 = null;
            HashMap v0 = new HashMap();
            ((Map)v0).put("mobile", android.support.design.a.h(((d$e)arg5).f));
            if(!TextUtils.isEmpty(v3)) {
                ((Map)v0).put("captcha", v3);
            }

            ((Map)v0).put("code", android.support.design.a.h(String.valueOf(((d$e)arg5).g)));
            ((Map)v0).put("mix_mode", "1");
            return ((Map)v0);
        }

        protected final void a(JSONObject arg4, c arg5) throws Exception {
            try {
                ((d$e)arg5).h = k.a(arg4);
            }
            catch(Exception v0) {
                ((d$e)arg5).a = bb.a().a((Context) this.a.get(), v0);
            }
        }
    }

    public static final class d$e extends c {
        public String f;
        public String g;
        public com.ss.android.account.b.k.k$a h;

        public d$e(String arg2, String arg3) {
            super(24);
            this.f = arg2;
            this.g = arg3;
        }
    }

    public static final class f extends d$b {
        public f(Context arg3, WeakHandler arg4, int arg5) {
            super(arg3, arg4, com.ss.android.account.aa.d, new g(arg5));
        }

        protected final Map a(c arg4) {
            HashMap v0 = new HashMap();
            ((Map)v0).put("type", String.valueOf(((g)arg4).e));
            return ((Map)v0);
        }

        protected final void a(JSONObject arg2, c arg3) throws Exception {
            ((g)arg3).f = arg2.optString("captcha");
        }
    }

    public static final class g extends c {
        public String f;

        public g(int arg1) {
            super(arg1);
        }
    }

    public static final class h extends d$b {
        public h(Context arg3, WeakHandler arg4, String arg5, String arg6, int arg7) {
            super(arg3, arg4, com.ss.android.account.aa.e, new i(arg5, arg6, arg7));
        }

        protected final Map a(c arg4) {
            HashMap v0 = new HashMap();
            ((Map)v0).put("mobile", android.support.design.a.h(((i)arg4).f));
            if(!TextUtils.isEmpty(((i)arg4).i)) {
                ((Map)v0).put("old_mobile", android.support.design.a.h(((i)arg4).i));
            }

            ((Map)v0).put("captcha", ((i)arg4).g);
            ((Map)v0).put("type", android.support.design.a.h(String.valueOf(((i)arg4).h)));
            ((Map)v0).put("unbind_exist", android.support.design.a.h("0"));
            ((Map)v0).put("mix_mode", "1");
            return ((Map)v0);
        }

        protected final void a(JSONObject arg3, c arg4) throws Exception {
            arg3.optInt("retry_time", 30);
        }
    }

    public static final class i extends c {
        public String f;
        public String g;
        public int h;
        public String i;

        public i(String arg2, String arg3, int arg4) {
            super(arg4);
            this.f = arg2;
            this.g = arg3;
            this.h = arg4;
            this.i = "";
        }
    }

    public WeakReference a;

    public d(Context arg3) {
        this.a = new WeakReference(arg3.getApplicationContext());
    }
}

